package algorithm.leetcode;

/**
 * @author bingo
 * @Description    删除链表指定元素,采用虚拟节点
 * @Date 2018/6/5
 */
public class Solution {
    private  ListNode  dummyHead = new ListNode(-1);
    public ListNode removeElements(ListNode head, int val){
         dummyHead.next = head;
         ListNode prev = dummyHead;
         while (prev.next!=null){
             if(prev.next.val==val){
                 ListNode delNode = prev.next;
                 prev.next = delNode.next;
             }else{
                 prev = prev.next;
             }

         }
         return  dummyHead.next;
     }

    public static void main(String[] args) {
        int[] arr = {1,2,3,6,4,5,6,8};
        ListNode listNode = new ListNode(arr);
        System.out.println(listNode.val);
        Solution solution = new Solution();
        System.out.println(listNode);
        listNode = solution.removeElements(listNode,6);
        System.out.println(listNode);
    }

}
